package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecGroupController {

    @Autowired
    SpecGroupService sgs;
    @Autowired
    SpecParamService sps;

    @GetMapping("groups/{cid}") // restFul
    public ResponseEntity<List<SpecGroup>> groups(@PathVariable("cid") Long cid){

        // 通过cid进行请求了
        List<SpecGroup> list = sgs.querySpecGroupByCid(cid);
        if(CollectionUtils.isEmpty(list)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);

    }

    // RequestMapping只能接受?拼接的参数形式
    // GetMapping 两种都可以
    // gid  规格参数组的id
    // cid  分类的id
    // 是否是search字段
    // 是否是通用字段
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> params(
            @RequestParam(value = "gid",required = false) Long gid,
            @RequestParam(value = "cid",required = false) Long cid,
            @RequestParam(value = "searching",required = false)Boolean searching,
            @RequestParam(value = "generic",required = false)Boolean generic
    ){
        List<SpecParam> list = sps.queryByGid(gid,cid,searching,generic);
        if(CollectionUtils.isEmpty(list)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);
    }

//    @PutMapping("group")
//
//    @PostMapping("group")
//
//    @DeleteMapping("group/{id}")


    @GetMapping("groupsAndParam/{cid}") // restFul
    public ResponseEntity<List<SpecGroup>> groupsAndParam(@PathVariable("cid") Long cid){

        // 通过cid进行请求了
        List<SpecGroup> list = sgs.querySpecGroupAndParamByCid(cid);
        if(CollectionUtils.isEmpty(list)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);

    }



}
